package cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord.vo.RfidChangeRecordPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord.vo.RfidChangeRecordRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord.vo.RfidChangeRecordSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.rfidchangerecord.RfidChangeRecordDO;
import cn.iocoder.yudao.module.hs.service.rfidchangerecord.RfidChangeRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - RFID更换记录")
@RestController
@RequestMapping("/hs/rfid-change-record")
@Validated
public class RfidChangeRecordController {

    @Resource
    private RfidChangeRecordService rfidChangeRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建RFID更换记录")
    @PreAuthorize("@ss.hasPermission('hs:rfid-change-record:create')")
    public CommonResult<Long> createRfidChangeRecord(@Valid @RequestBody RfidChangeRecordSaveReqVO createReqVO) {
        return success(rfidChangeRecordService.createRfidChangeRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新RFID更换记录")
    @PreAuthorize("@ss.hasPermission('hs:rfid-change-record:update')")
    public CommonResult<Boolean> updateRfidChangeRecord(@Valid @RequestBody RfidChangeRecordSaveReqVO updateReqVO) {
        rfidChangeRecordService.updateRfidChangeRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除RFID更换记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:rfid-change-record:delete')")
    public CommonResult<Boolean> deleteRfidChangeRecord(@RequestParam("id") Long id) {
        rfidChangeRecordService.deleteRfidChangeRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得RFID更换记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:rfid-change-record:query')")
    public CommonResult<RfidChangeRecordRespVO> getRfidChangeRecord(@RequestParam("id") Long id) {
        RfidChangeRecordDO rfidChangeRecord = rfidChangeRecordService.getRfidChangeRecord(id);
        return success(BeanUtils.toBean(rfidChangeRecord, RfidChangeRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得RFID更换记录分页")
    @PreAuthorize("@ss.hasPermission('hs:rfid-change-record:query')")
    public CommonResult<PageResult<RfidChangeRecordRespVO>> getRfidChangeRecordPage(@Valid RfidChangeRecordPageReqVO pageReqVO) {
        PageResult<RfidChangeRecordDO> pageResult = rfidChangeRecordService.getRfidChangeRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RfidChangeRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出RFID更换记录 Excel")
    @PreAuthorize("@ss.hasPermission('hs:rfid-change-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRfidChangeRecordExcel(@Valid RfidChangeRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RfidChangeRecordDO> list = rfidChangeRecordService.getRfidChangeRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "RFID更换记录.xls", "数据", RfidChangeRecordRespVO.class,
                        BeanUtils.toBean(list, RfidChangeRecordRespVO.class));
    }

}